package com.ggstream.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.tree.DefaultMutableTreeNode;

import com.ggstream.Application;
import com.ggstream.PageScanResult;

public class PanneauProjet extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField txtHttp;
	private JTextField txtHttp_1;
	private DefaultMutableTreeNode root = new DefaultMutableTreeNode("Site Web");

	public DefaultMutableTreeNode getRoot() {
		return root;
	}

	/**
	 * Create the panel.
	 */
	public PanneauProjet() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel.setBackground(new Color(255, 248, 220));
		add(panel, BorderLayout.NORTH);
		
		JLabel lblProjet = new JLabel("Projet");
		lblProjet.setFont(new Font("Arial Black", Font.BOLD, 16));
		lblProjet.setHorizontalAlignment(SwingConstants.CENTER);
		panel.add(lblProjet);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		add(tabbedPane, BorderLayout.CENTER);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Sites Web", null, panel_1, null);
		panel_1.setLayout(null);
		
		txtHttp = new JTextField();
		txtHttp.setText("http://");
		txtHttp.setBounds(10, 33, 249, 20);
		panel_1.add(txtHttp);
		txtHttp.setColumns(10);
		
		JCheckBox checkBox = new JCheckBox("");
		checkBox.setBounds(276, 32, 27, 23);
		panel_1.add(checkBox);
		
		JButton btnValider = new JButton("valider");
		btnValider.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				validerSiteWeb(0) ;
			}
		});
		btnValider.setBounds(309, 32, 127, 23);
		panel_1.add(btnValider);
		
		txtHttp_1 = new JTextField();
		txtHttp_1.setText("http://");
		txtHttp_1.setColumns(10);
		txtHttp_1.setBounds(10, 66, 249, 20);
		panel_1.add(txtHttp_1);
		
		JCheckBox checkBox_1 = new JCheckBox("");
		checkBox_1.setBounds(276, 65, 27, 23);
		panel_1.add(checkBox_1);
		
		JButton button = new JButton("valider");
		button.setBounds(309, 65, 127, 23);
		panel_1.add(button);
		
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("musiques", null, panel_2, null);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		JTree tree = new JTree(root);
		panel_2.add(tree, BorderLayout.CENTER);
		
		JLabel lblArborescence = new JLabel("Arborescence");
		lblArborescence.setHorizontalAlignment(SwingConstants.CENTER);
		lblArborescence.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 14));
		panel_2.add(lblArborescence, BorderLayout.NORTH);

	}

	protected void validerSiteWeb(int idWebSite) {
		switch (idWebSite) {
		case 0:
			String URL = txtHttp.getText() ;
			constructTreeRecursively(root, URL) ;
			break;

		default:
			break;
		}
	}

	private void constructTreeRecursively(
			DefaultMutableTreeNode node,
			String URL) {

		PageScanResult page = Application.analyseWebPage(URL) ;
		for (String link : page.getLinksSubWebsite()) {
			DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new String(link)) ;
			node.add(newNode) ;
			newNode.setAllowsChildren(true) ;
			constructTreeRecursively(newNode, link) ;
		}
		for (String link : page.getLabs()) {
			node.add(new DefaultMutableTreeNode(new String(link))) ;
		}
	}
}
